/**
 * 	<script type="text/javascript" src="javascript/video/ckplayer6.7/ckplayer/ckplayer.js" charset="utf-8"></script>
 */
Ext.define('Sh.ux.VideoPlayer', {
	extend : 'Ext.panel.Panel',
	alias: 'widget.player',
	layout: 'border',
	config: {
		autoPlay: false,
		isplay: false,
		autoLoadVideo: true,
		loaded: false,
		oldtime: 0
	},
	loadedVideo: function(){
		this.setLoaded(true);
	},
	initComponent : function(config) {
		var me = this;
		me.on('boxready',function(p){
			me.initVideo();
			me.initMenu();
		});
		
		me.on('resize',function(p,w,h,ow,oh){
			if(this.loaded){
				try {
					me.setVideoSize(this.body.getWidth()-2.5,this.body.getHeight()-5);
				} catch (e) {
					// TODO: handle exception
				}
			}
		});
		
		me.callParent();
	},
	initVideo: function(){
		var me = this;
		var id = this.body.dom.id;
		var vwidth = me.body.getWidth()-2.5;
		var vheight = me.body.getHeight()-5;
		var flashvars={
			f: me.src,
			a:'',
			s:'0',
			c:'0',
			x:'',
			u:'',
			e:'3',
			v:'80',
			h:'0',
			q:'',
			g: me.oldtime ? me.oldtime : 0,
			p: me.autoPlay ? '1' : (me.autoLoadVideo ? '0' : '2'),
			loaded: "function(){Ext.getCmp('"+me.id+"').loadedVideo();}"
		};
		var params= {bgcolor:'#FFF',allowFullScreen:true,allowScriptAccess:'always',wmode:'transparent'};
		var a = CKobject.embedSWF('javascript/ckplayer/ckplayer.swf',id,me.id+'-player',vwidth,vheight,flashvars,params);
		var video=[];
		var support=['iPad','iPhone','ios','android+false','msie10+false'];
		var b = CKobject.embedHTML5(id,id+'-player',vwidth,vheight,video,flashvars,support);
//		if(me.oldtime > 0){
//			me.goToTime(me.oldtime);
//		}
	},
	initMenu: function(){
		var me = this;
		me.contextmenu = Ext.create('Ext.menu.Menu',{
			width: 100,
			minWidth: 100,
			items: [{
				text: '播放',
				iconCls: 'icon-play',
				handler: function(){
					me.play();
				}
			},{
				text: '暂停',
				iconCls: 'icon-pause',
				handler: function(){
					me.pause();
				}
			},{
				text: '停止',
				iconCls: 'icon-stop',
				handler: function(){
					me.currentTime(0);
					me.pause();
				}
			}]
		});
		me.body.on('contextmenu',function(e,t){
			e.preventDefault();
			me.contextmenu.showAt(e.getXY());
		});
	},
	removeVideo: function(){
		var me = this;
		this.loaded = false;
		try {
			this.setOldtime(me.getCurrentTime());
			this.getPlayer().videoClear();
			$('*',this.body.dom).remove();
		} catch (e) {
			// TODO: handle exception
		}
	},
	isPlay: function(){
		return this.getPlayer().getStatus().play;
	},
	playOrPause: function(){
		this.getPlayer().playOrPause(); 
	},
	getPlayer: function(){
		return CKobject.getObjectById(this.id+'-player');
	},
	play: function(){
		this.getPlayer().videoPlay();
	},
	pause: function(){
		this.getPlayer().videoPause();
	},
	goToTime: function(t){
		this.getPlayer().videoSeek(t);
	},
	getCurrentTime: function(){
		return this.getPlayer().getStatus().time;
	},
	videoClear: function(){
		this.getPlayer().videoClear();
	},
	setVideoSize: function(width,height){
		this.getPlayer().width = width ? width : this.getPlayer().width;
		this.getPlayer().height = height ? height : this.getPlayer().height;
		
	},
	screenShot: function(){
		return this.getPlayer().screenShot("video",0,0,0,0);
	}
});